package com.bw.kotlindemo.kotlin.base

import android.content.Context
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModel

/**
 * desc:
 * create by xushaodong on 2023/4/6
 */
abstract class KBaseFragment<VM : ViewModel, DBD : ViewDataBinding> : Fragment() {
    //创建
    lateinit var mViewModel: VM
    lateinit var mDataBinding: DBD
    lateinit var activity: Context

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        activity = requireActivity()
    }

    //创建
    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {

        //获取页面
        mDataBinding = DataBindingUtil.inflate(inflater, initLayout(), container, false)
        return mDataBinding.getRoot()
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        mViewModel = initViewModel()
        initData()
    }

    protected abstract fun initLayout(): Int

    protected abstract fun initData()

    abstract fun initViewModel(): VM

}